define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    // add_url: 'user/user/add',
                    // edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    // multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'group.name', title: __('Group')},
                        // {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            operate: false
                        },
                        // {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'money', title: __('Money'), operate: false},
                        {field: 'score', title: __('Score'), operate: false},
                        // {field: 'record', title: __('Record'),table: table,
                        //     buttons: [{
                        //         name: 'Record',text: "消费记录",title: "消费记录",classname: 'btn btn-xs btn-primary btn-dialog',url:'user/userconsume/index'
                        //     }],
                        //     formatter: function (value,row,index){
                        //         var that = $.extend({}, this);
                        //         var table = $(that.table).clone(true);
                        //         $(table).data("operate-edit", null);
                        //         $(table).data("operate-del", null);
                        //         that.table = table;
                        //         return Table.api.formatter.operate.call(that, value, row, index);
                        //     },
                        //     events:Table.api.events.operate,operate:false
                        // },
                        // {
                        //     field: 'record',
                        //     width: "150px",
                        //     title: __('Record'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     buttons: [
                        //         {
                        //             name: 'record',
                        //             text: "消费记录",
                        //             title: '消费记录',
                        //             classname: 'btn btn-xs btn-primary btn-dialog',
                        //             icon: 'fa fa-list',
                        //             url: 'user/user/record',
                        //             callback: function (data) {
                        //                 Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                        //             }
                        //         },
                        //     ],
                        //     formatter: Table.api.formatter.operate
                        // },
                        {field: 'team', title: __('Team'), operate: false},
                        {
                            field: 'status',
                            title: __('Status'),
                            operate: false,
                            formatter: Controller.api.formatter.custom
                        },
                        // {field: 'status', title: __('Status'), searchList: {"1":__('No'),"2":__('Yes')}, formatter: Table.api.formatter.toggle},
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'vip_card', title: __('vipCard')},
                        {field: 'vip_card_time', title: __('vipCardTime')},

                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'record',
                                    text: "设置会员",
                                    title: '设置会员',
                                    classname: 'btn btn-info btn-xs btn-click',
                                    click: function (data, row) {
                                        Layer.confirm(
                                            '确定给该用户开通会员吗?',
                                            {icon: 3, title: '开通会员',btn: ['确定', '取消']},
                                            function (index) {
                                                Fast.api.ajax({
                                                    url: 'user/user/setVip',
                                                    type: 'post',
                                                    data: {id: row.id},
                                                    loading: true,
                                                }, function () {
                                                    table.bootstrapTable('refresh');
                                                    Layer.close(index);
                                                },function () {
                                                    Toastr.error("失败");
                                                });
                                            }
                                        );
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        },

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法

                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="user/user/change" data-confirm="确定审核？" data-id="' + row.id + '"><i class="fa ' + (row.status == '' ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
        }
    };
    return Controller;
});